﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq;
using System.Text;
using JKF.BusinessLayer.BLL;
using JKF.DataModule;
using JKF.DataModule.SqlServerDAL;
using JKF.Entities.Model;
using JKF.Substructure;

namespace JKF.BusinessLayer.BLL
{
    public partial class bllSYS_LOG
    {
        public DataTable LogSearch(string searchItem, string matchWay, string keyword, string DATE_START, string DATE_END,
         string moduleName,int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            dalSYS_LOG sysLog = new dalSYS_LOG();
            string userKey = bllSYS_USER.CurrentUser.USER_KEY.ToString();
            return sysLog.LogSearch(searchItem, matchWay, keyword, DATE_START,  DATE_END,
            moduleName, userKey, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }

        public class SysLogItem
        {
            public string FIELD_NAME;
            public string FIELD_DES;
            public string OLD_VALUE;
            public string NEW_VALUE;
        }

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="moduleCode">模块编码 SYS_MODULE.MODULE_CODE</param>
        /// <param name="tableName">操作的表名</param>
        /// <param name="primaryKey">操作的记录 主键值</param>
        /// <param name="logContent"></param>
        /// <param name="operationType">添加/删除/修改/查询/登录</param>
        /// <param name="ip">访问ip</param>
        /// <returns>true 日志添加成功  false 日志添加失败</returns>
        public static bool SaveLog(string moduleCode,string tableName,string primaryKey,string logContent
            , string operationType, string ip)
        {
            try
            {
                var cmdList = new List<DbCommand>();
                var dbClass = DB.HR;

                string logKey = DB.GetNewGuid(DB.HR);

                SYS_LOGinfo sysLoginInfo = new SYS_LOGinfo();
                var dalSysLogInfo = new dalSYS_LOG();
                sysLoginInfo.LOG_KEY = new Guid(logKey);
                sysLoginInfo.MODULE_CODE = moduleCode;
                sysLoginInfo.TABLE_NAME = tableName;
                sysLoginInfo.PRIMARY_KEY = primaryKey;
                sysLoginInfo.LOG_CONTENT = logContent;
                sysLoginInfo.OPERATION_TYPE = operationType;
                sysLoginInfo.IP = ip;
                sysLoginInfo.SHOP_CODE = bllSYS_USER.CurrentUser.SHOP_CODE;
                sysLoginInfo.USER_CREATE = bllSYS_USER.CurrentUser.USER_KEY.ToString();
                cmdList.Add(dalSysLogInfo.GetAddDbCommand(sysLoginInfo));

                /*
                if (itemArr != null)
                {
                    const string SQL_SYS_LOG_ITEM_INSERT = "INSERT INTO SYS_LOG_ITEM(LOG_KEY,FIELD_NAME,FIELD_DES,OLD_VALUE,NEW_VALUE)VALUES(@LOG_KEY,@FIELD_NAME,@FIELD_DES,@OLD_VALUE,@NEW_VALUE)";
                    foreach (var tmpItem in itemArr)//添加兑换明细
                    {
                        var cmd = dbClass.GetSqlStringCommand(SQL_SYS_LOG_ITEM_INSERT);
                        dbClass.AddInParameter(cmd, "LOG_KEY", DbType.Guid, new Guid(logKey));
                        dbClass.AddInParameter(cmd, "FIELD_NAME", DbType.String, tmpItem.FIELD_NAME);
                        dbClass.AddInParameter(cmd, "FIELD_DES", DbType.String, tmpItem.FIELD_DES);
                        dbClass.AddInParameter(cmd, "OLD_VALUE", DbType.String, tmpItem.OLD_VALUE);
                        dbClass.AddInParameter(cmd, "NEW_VALUE", DbType.String, tmpItem.NEW_VALUE);
                        cmdList.Add(cmd);
                    }
                }*/
                ExecTransaction.Exec(cmdList);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
